<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/4/15
 * Time: 19:48
 */

namespace frontend\controllers;


use backend\models\Product;
use yii\data\Pagination;
use yii\web\Controller;

class ProductController extends CommonController
{
public $layout = 'layout1';

    public function actionIndex()
    {
        $id = \Yii::$app->request->get('cateid');
        $model = Product::find()->where(['cateid'=>$id]);
        $count = $model->count();

        $pager = new  Pagination(['totalCount'=>$count,'pageSize'=>3]);

        $all = $model->limit($pager->limit)->offset($pager->offset)->asArray()->all();
        $tui = $model->Where(['istui'=>1,'cateid'=>$id])->orderby('createtime desc')->limit(5)->asArray()->all();
        $hot = $model->Where(['ishot'=>1,'cateid'=>$id])->orderby('createtime desc')->limit(5)->asArray()->all();
        $sale = $model->Where(['issale'=>1,'cateid'=>$id])->orderby('createtime desc')->limit(5)->asArray()->all();
        return $this->render("index", ['sale' => $sale, 'tui' => $tui, 'hot' => $hot, 'all' => $all, 'pager' => $pager, 'count' => $count]);
    }

    public function actionDetail()
    {
        $id = \Yii::$app->request->get('productid');
        $product = Product::find()->where(['productid'=>$id])->asArray()->one();
        $data['all'] = Product::find()->where('ison = "1"')->orderby('createtime desc')->limit(7)->all();
//        var_dump($product);
//        die;
        return $this->render('detail',['product'=>$product,'data'=>$data]);
    }
}